package qlsl.androiddesign.view.subview.activityview;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.activity.commonactivity.EditActivity;
import qlsl.androiddesign.activity.subactivity.RepairShoppingReleaseActivity;
import qlsl.androiddesign.adapter.subadapter.RecruitReleasAdapter;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.constant.IntentCodeConstant;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.http.xutils.Protocol.FileType;
import qlsl.androiddesign.listener.OnUploadFileListener;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.util.commonutil.FileNameUtils;
import qlsl.androiddesign.util.runfeng.SelectServerTypeDialog;
import qlsl.androiddesign.view.subview.activityview.peoplecenterbase.ListWapBaseView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONObject;
import com.photoselector.model.PhotoModel;
import com.photoselector.ui.PhotoSelectorActivity;
import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 快修发布页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class RepairShoppingReleaseView extends
		ListWapBaseView<HashMap<String, Object>, RepairShoppingReleaseActivity> {

	private List<File> files = new ArrayList<File>();

	private SelectServerTypeDialog dialog;

	public RepairShoppingReleaseView(RepairShoppingReleaseActivity activity) {
		super(activity);
	}

	protected void initView(View view) {
		super.initView(view);
		setTitle("商家发布");
	}

	protected void initData() {
		setListViewData();
		super.initData();

	}

	private void setListViewData() {
		String[] names = new String[] { "商家发布", "服务类型", "商家地址", "商家电话", "老板姓名",
				"添加图片" };
		String[] values = new String[] {
				"不限",
				"",
				"" + UserMethod.getUser().getProvincename()
						+ UserMethod.getUser().getCityname()
						+ UserMethod.getUser().getAreaname(),
				"" + UserMethod.getUser().getMobile(),
				"" + UserMethod.getUser().getUsername(), "" };

		for (int index = 0; index < names.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("text", names[index]);
			map.put("value", values[index]);
			list.add(map);
		}
	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);
			break;
		case R.id.send:
			doClickSendPost(view);
			break;
		case R.id.dialog_list_item:
			doClickDialogListItem(view);
			break;
		}

	}

	private void doClickDialogListItem(View view) {
		dialog.dismiss();
		String value = (String) this.dialog.getSelectItem(view);
		list.get(1).put("value", value);
		notifyDataSetChanged();
	}

	private void doClickSendPost(View view) {
		for (HashMap<String, Object> map : list) {
			Object value = map.get("value");
			if (value == null || TextUtils.isEmpty(value.toString())) {
				showToast("请输入");
				return;
			}
		}
		String[] keys = new String[] { "businessname", "servertype", "address",
				"mobile", "bossname" };

		HttpProtocol protocol = new HttpProtocol();
		protocol.setMethod("publishquicklyrepair");

		for (int index = 0, count = keys.length; index < count; index++) {
			HashMap<String, Object> values = list.get(index);

			if (keys.length - 1 == index) {
				HashMap<String, Object> values2 = list.get(0);
				protocol.addParam(keys[index], values2.get("value"));
			} else {
				protocol.addParam(keys[index], values.get("value"));
			}
		}
		protocol.addParam("province", "" + Constant.province);
		protocol.addParam("city", "" + Constant.city);

		protocol.addParam("longitude", "" + Constant.longitude);
		protocol.addParam("latitude", "" + Constant.latitude);
		protocol.addParam("type", "1");

		protocol.addParam("fuserid", UserMethod.getUser().getUserId());
		protocol.removeUserParam();

		if (files.size() > 0) {
			setProgressBarText("正在发布");
			showProgressBar();
			protocol.uploadFiles(files, FileType.PICTURE, listener);
		} else {
			showToast("请上传图片");
		}
	}

	private void showQuickOption() {
		dialog = new SelectServerTypeDialog(activity);
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

	private void doClickListItem(View view) {

		int position = listView.getPositionForView(view);
		listView.setTag(position);
		if (listView.getCount() - 1 == position) {
			doClickSelectPhoto();
		} else if (1 == position) {
			showQuickOption();
		} else {
			@SuppressWarnings("unchecked")
			HashMap<String, Object> map = (HashMap<String, Object>) listView
					.getAdapter().getItem(position);

			String text = (String) map.get("text");
			String value = (String) map.get("value");

			Intent intent = new Intent(activity, EditActivity.class);
			intent.putExtra("title", "编辑" + text);
			intent.putExtra("content", value);
			intent.putExtra("maxLength", 11);
			startActivityForResult(intent, 0);
		}

	}

	public void doClickSelectPhoto() {
		Intent intent = new Intent(activity, PhotoSelectorActivity.class);
		intent.putExtra(PhotoSelectorActivity.KEY_MAX, 10);
		intent.putExtra(PhotoSelectorActivity.KEY_SECOND_DIR,
				FileNameUtils.getAccountSecondDir());

		intent.putExtra(PhotoSelectorActivity.KEY_PREFIX_NAME,
				FileNameUtils.getAccountPrefixName(activity));
		startActivityForResult(intent,
				IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED);
	}

	public BaseAdapter getAdapter() {
		return new RecruitReleasAdapter(activity, list);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		HashMap<String, Object> map = (HashMap<String, Object>) listView
				.getAdapter().getItem((Integer) listView.getTag());
		if (requestCode == 0) {
			if (data != null) {
				map.put("value", data.getStringExtra("content"));
			}
		} else if (requestCode == IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle bundle = data.getExtras();
				if (bundle == null) {
					showToast("无法加载图片");
					return;
				}
				List<PhotoModel> selectPhotos = (List<PhotoModel>) bundle
						.getSerializable(PhotoSelectorActivity.KEY_RESULT);
				if (selectPhotos.size() > 0) {
					for (PhotoModel photoModel : selectPhotos) {
						files.add(new File(photoModel.getOriginalPath()));
					}
					map.put("value", selectPhotos.size() + "/9");
				}
			}
		}
		notifyDataSetChanged();
	}

	private OnUploadFileListener listener = new OnUploadFileListener() {

		public void onSuccess(JSONObject jo) {
			showToast("发布成功");
			setRefresh(true);
			activity.finish();
		}

	};

}
